<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class mModel extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function insertRadius($tabel, $data) {
        $radius = $this->load->database('default', TRUE);
        $radius->insert($tabel, $data);
    }

    function insertSms($tabel, $data) {
        $sms = $this->load->database('sms', TRUE);
        $sms->insert($tabel, $data);
    }

    function updateSms($tabel, $id, $data) {
        $sms = $this->load->database('sms', TRUE);
        $sms->where('ID', $id);
        $sms->update($tabel, $data);
    }

    function updateRadius($tabel, $username, $data) {
        $sms = $this->load->database('default', TRUE);
        $sms->where('username', $username);
        $sms->update($tabel, $data);
    }

    function cekSms() {
        $sms = $this->load->database('sms', TRUE);

        $sms->select('TextDecoded,SenderNumber,ID');
        $sms->from('inbox');
        $sms->where('processed', 'false');

        return $sms->get();
    }

    function getExpiration($user) {
        $radius = $this->load->database('default', TRUE);

        $radius->select('value');
        $radius->from('radcheck');
        $radius->where('username', $user);
        $radius->where('attribute', 'Expiration');

        return $radius->get();
    }

    function getListExpiration() {
        $radius = $this->load->database('default', TRUE);

        $radius->select('username,value');
        $radius->from('radcheck');
        $radius->where('attribute', 'Expiration');

        return $radius->get();
    }

    function valUserNumber($no, $user) {
        $radius = $this->load->database('default', TRUE);

        $radius->select('username');
        $radius->from('user');
        $radius->where('username', $user);
        $radius->or_where('telpon');

        return $radius->get();
    }

    function getPhoneNumber($user) {
        $radius = $this->load->database('default', TRUE);

        $radius->select('telpon');
        $radius->from('user');
        $radius->where('username', $user);

        return $radius->get();
    }

    function getNotifStatus($user) {
        $radius = $this->load->database('default', TRUE);

        $radius->select('notif');
        $radius->from('user');
        $radius->where('username', $user);
        $radius->where('notif', 'false');

        return $radius->get();
    }

    function getSMS() {
        $sms = $this->load->database('sms', TRUE);

        $sms->select('*');
        $sms->from('inbox');

        $q = $sms->get();

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function getUser() {
        $radius = $this->load->database('default', TRUE);

        $radius->select('*');
        $radius->from('radcheck');
        $radius->order_by('id', 'desc');

        $q = $radius->get();

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function user_page($limit, $start) {
        $this->db->limit($limit, $start);
        return $this->db->order_by('id', 'desc')->get_where('radcheck', array('attribute' => "Expiration"), $limit, $start)->result();
    }

    function jumlahbaris() {
        //return $this->db->count_all('radcheck', array('attribute' => "Expiration"));
        $query = $this->db->select("*")
                ->from('radcheck')
                ->where('attribute', 'Expiration')
                ->get();
        return $query;
    }

    function telpon() {
        //$query = $this->db->query("SELECT * FROM user ORDER BY no DESC");
        //return $query->result();
        $radius = $this->load->database('default', TRUE);

        $radius->select('*');
        $radius->from('user');
        $radius->order_by('no', "desc");

        $q = $radius->get();

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function profile() {
        $radius = $this->load->database('default', TRUE);

        $radius->select('*');
        $radius->from('radreply');
        $radius->order_by("id", "desc");

        $q = $radius->get();

        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }

    function edit_user($no) {
        $q = "SELECT * FROM radcheck WHERE id='$no'";
        $query = $this->db->query($q);
        return $query->row();
    }

    function simpan_edit_user($no, $username, $telpon) {
        $data = array(
            'no' => $no,
            'username' => $username,
            'telpon' => $telpon
        );
        $this->db->where('no', $no);
        $this->db->update('user', $data);
    }

    function simpan_user_data() {
        
    }

    function joinUser() {
        $sql = "SELECT user.* FROM user INNER JOIN radcheck ON user.username = radcheck.username";
    }

    function hapusUser($no) {
        $sql = "DELETE t1, t2 FROM radcheck t1 JOIN user t2 ON t1.username=t2.username WHERE t1.id='$no'";
        $q = $this->db->query($sql);
        return $q;
    }

    function hapusSemua() {
        $sql = "truncate table radcheck";
        $q = $this->db->query($sql);
    }

    function hapusSemuaUser() {
        $sql = "truncate table user";
        $q = $this->db->query($sql);
    }

    function hapusSemuaprofile() {
        $sql = "truncate table radreply";
        $q = $this->db->query($sql);
    }

    function caridata() {
        $c = $this->input->POST('cari');
        $this->db->like('username', $c);
        $query = $this->db->get('user');
        return $query->result();
    }

}
